import axios from 'axios';
import fs from 'fs';

const fileId = '7ryM2G71XRVwkQnkklWrbI';
const nodeId = '0:2671'; // 号码登录页面
const token = process.env.FIGMA_ACCESS_TOKEN;

console.log('🔍 详细分析节点 2671 的布局结构...');

try {
  const response = await axios.get(`https://api.figma.com/v1/files/${fileId}/nodes`, {
    headers: { 'X-Figma-Token': token },
    params: { ids: nodeId }
  });
  
  const nodeData = response.data.nodes[nodeId];
  
  if (nodeData && nodeData.document) {
    const doc = nodeData.document;
    console.log(`\n📱 页面: ${doc.name}`);
    console.log(`尺寸: ${doc.absoluteBoundingBox.width} x ${doc.absoluteBoundingBox.height}`);
    console.log(`背景色: ${doc.backgroundColor ? JSON.stringify(doc.backgroundColor) : '无'}`);
    
    console.log('\n📋 子元素详细分析:');
    
    doc.children.forEach((child, index) => {
      const box = child.absoluteBoundingBox;
      console.log(`\n${index + 1}. ${child.name} (${child.type})`);
      console.log(`   位置: x=${box.x}, y=${box.y}`);
      console.log(`   尺寸: ${box.width} x ${box.height}`);
      
      // 计算相对位置（相对于页面顶部）
      const relativeY = box.y - doc.absoluteBoundingBox.y;
      const relativeX = box.x - doc.absoluteBoundingBox.x;
      console.log(`   相对位置: x=${relativeX}, y=${relativeY}`);
      
      // 分析文本内容
      if (child.type === 'TEXT' && child.characters) {
        console.log(`   文本内容: "${child.characters}"`);
        if (child.style) {
          console.log(`   字体大小: ${child.style.fontSize || '未知'}`);
          console.log(`   字体粗细: ${child.style.fontWeight || '未知'}`);
        }
      }
      
      // 分析颜色
      if (child.fills && child.fills.length > 0) {
        const fill = child.fills[0];
        if (fill.type === 'SOLID') {
          const color = fill.color;
          const hex = `#${Math.round(color.r * 255).toString(16).padStart(2, '0')}${Math.round(color.g * 255).toString(16).padStart(2, '0')}${Math.round(color.b * 255).toString(16).padStart(2, '0')}`;
          console.log(`   颜色: ${hex}`);
        }
      }
      
      // 分析子元素
      if (child.children && child.children.length > 0) {
        console.log(`   包含 ${child.children.length} 个子元素:`);
        child.children.forEach((subChild, subIndex) => {
          console.log(`     ${subIndex + 1}. ${subChild.name} (${subChild.type})`);
          if (subChild.characters) {
            console.log(`        文本: "${subChild.characters}"`);
          }
        });
      }
    });
    
    // 保存详细数据
    fs.writeFileSync('layout-analysis.json', JSON.stringify(nodeData, null, 2));
    console.log('\n💾 详细数据已保存到 layout-analysis.json');
    
  }
  
} catch (error) {
  console.log('❌ 分析失败:', error.response?.status, error.response?.statusText);
  console.log('错误详情:', error.response?.data);
}
